package com.ruoyi.web.controller.map;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.MapPark;
import com.ruoyi.system.service.IMapParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产业园区Controller
 *
 * @author cqust_icc
 * @date 2022-06-25
 */
@RestController
@RequestMapping("/system/park")
public class MapParkController extends BaseController {
    @Autowired
    private IMapParkService mapParkService;

    /**
     * 查询产业园区列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MapPark mapPark)
    {
        startPage();
        List<MapPark> list = mapParkService.selectMapParkList(mapPark);
        return getDataTable(list);
    }

    /**
     * 导出产业园区列表
     */
    @PreAuthorize("@ss.hasPermi('system:park:export')")
    @Log(title = "产业园区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MapPark mapPark)
    {
        List<MapPark> list = mapParkService.selectMapParkList(mapPark);
        ExcelUtil<MapPark> util = new ExcelUtil<MapPark>(MapPark.class);
        util.exportExcel(response, list, "产业园区数据");
    }

    /**
     * 获取产业园区详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:park:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mapParkService.selectMapParkById(id));
    }

    /**
     * 新增产业园区
     */
    @PreAuthorize("@ss.hasPermi('system:park:add')")
    @Log(title = "产业园区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MapPark mapPark)
    {
        return toAjax(mapParkService.insertMapPark(mapPark));
    }

    /**
     * 修改产业园区
     */
    @PreAuthorize("@ss.hasPermi('system:park:edit')")
    @Log(title = "产业园区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MapPark mapPark)
    {
        if (!StringUtils.isEmpty(mapPark.getCloudLink())) {
            if (!StringUtils.ishttp(mapPark.getCloudLink())) {
                return AjaxResult.error("新增失败，地址必须以http(s)://开头");
            }
        }
        return toAjax(mapParkService.updateMapPark(mapPark));
    }

    /**
     * 删除产业园区
     */
    @PreAuthorize("@ss.hasPermi('system:park:remove')")
    @Log(title = "产业园区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mapParkService.deleteMapParkByIds(ids));
    }
}
